package com.gis3c.spatial.web.controller;

import com.gis3c.online.entry.SurfaceWaterStation;
import com.gis3c.online.service.SurfaceWaterService;
import com.gis3c.spatial.service.RegionService;
import com.gis3c.spatial.service.RiverService;
import com.gis3c.sys.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by hukekuan on 2017/12/7.
 */
@RestController
@RequestMapping(value = "/api/spatial")
public class SpatialController extends BaseController {
    @Autowired
    RegionService regionService;
    @Autowired
    RiverService riverService;
    @Autowired
    private SurfaceWaterService surfaceWaterService;


    @Value("#{configProperties['sys.renginCode']}")
    protected String renginCode;

//  ==================================行政区接口 start =======================================
    @RequestMapping(value = "/region/entry", method = RequestMethod.GET)
    public Object RegionEntry(){
        return regionService.findRegionByCode(renginCode);
    }

    @RequestMapping(value = "/region/center", method = RequestMethod.GET)
    public Object RegionCenter(){
        return regionService.findRgionCenterByCode(renginCode);
    }

    @RequestMapping(value = "/region/childregions", method = RequestMethod.GET)
    public Object ChildRegions(){
        return regionService.findRegionsByParentCode(renginCode);
    }

    @RequestMapping(value = "/region/childcenters", method = RequestMethod.GET)
    public Object ChildRgionCenters(){
        return regionService.findRegionCentersByParentCode(renginCode);
    }

//  ==================================行政区接口 end =========================================

//  ==================================河流接口 start =======================================
    @RequestMapping(value = "/river/separatedriver", method = RequestMethod.GET)
    public Object findSeparatedRiver(){
        List<SurfaceWaterStation> surfaceWaterStationList = surfaceWaterService.findSurfaceWaterStations();
        Set<String> riverCodes = new HashSet<>();
        Map<String,String> riverLevels = new HashMap<>();
        surfaceWaterStationList.forEach(surfaceWaterStation -> {
            if(!riverCodes.contains(surfaceWaterStation.getRiverCode())){
                riverCodes.add(surfaceWaterStation.getRiverCode());
            }
            riverLevels.put(
                    surfaceWaterStation.getStationId(),
                    surfaceWaterStation.getLevel() == null || "".equals(surfaceWaterStation.getLevel()) ? "--":surfaceWaterStation.getLevel()
            );
        });
        return riverService.findRiversByRiverCodes(riverCodes,riverLevels);
    }

    @RequestMapping(value = "/river/modelriverinfo", method = RequestMethod.GET)
    public Object findModelRiverInfo(){
        return riverService.findModelRiverInfo();
    }

    @RequestMapping(value = "/river/modelriver", method = RequestMethod.GET)
    public Object findModelRiver(@RequestParam(value = "riverName",required = true) String riverName){
        return riverService.findModelRiverByName(riverName);
    }
//  ==================================河流接口 end =========================================
}
